#include "display_dir.h"
#include "display_dev.h"


/*
/*驱动 display_dir 文件
/*实现 Display_dir 结构体
/*实现对应的函数
*/
Display_dir current_dir = {
	.name = "display_dir" ,
	.find_dev		= find_name_to_dev,
	.bind_dev       = bind_current_dev,
	.init_dev       = init_current_dev,
	.get_dev_args	= get_current_dev_args,
	.clear_show 	= dev_clear_show,
	.show           = dir_dev_show,
	.show_Font      = show_Freetype,
	.show_Font_Str  = show_Freetype_Str,
};

/*
/*通过名字寻找设备，绑定设备
/*并通过设备反绑驱动
*/

static pDisplay_dev find_name_to_dev(char * name){
	
	pDisplay_dev buf_dev = head_dev->next;
	if(head_dev == NULL) {
		printf("this err\n");
	}
	while(buf_dev != NULL && strcmp(name,buf_dev->name) !=0) {
		buf_dev = buf_dev->next;
	}
	if(buf_dev != NULL) { 
		current_dir.pcurrent_dev = buf_dev;
		current_dir.bind_dev(current_dir.name);
		return buf_dev;  //div 绑定 dev
	}
	return NULL;
}

/*
/*反绑函数
/*传入驱动名字，调用底层设备绑定驱动的函数
*/

static int bind_current_dev(char *name){

	if(current_dir.pcurrent_dev->bind_dir(name) != 0) {  //dev 绑定  div
		printf("the bind_current_dev err \n");
		return -1;  
	}
	return 0;
}

/*
/*初始化设备函数
/*调用底层设备初始化驱动的函数
*/

static int init_current_dev(pDisplay_dev pcurrent_dev) {
	
	if(pcurrent_dev->init_dev() < 0) {

		printf("div init dev err \n");
		return -1;
	}
	return 0;
}

/*
/*初始化设备函数
/*调用底层设备初始化获取设备参数的函数
*/

static pDisplay_dev_arg_t get_current_dev_args() { 
	pDisplay_dev_arg_t pDisplay_dev_arg_t_buf = current_dir.pcurrent_dev->get_args();
	if(pDisplay_dev_arg_t_buf == NULL) {
		printf("get_current_dev_args err \n");
	} 
	return pDisplay_dev_arg_t_buf;
}

/*
/*画图函数：描点
/*调用底层设备描点函数
*/

static void dir_dev_show(pDisplay_dev pcurrent_dev,int x,int y,unsigned int color) {
	pcurrent_dev->show_pix(x,y,color);
}
  
static int show_Freetype(pFontBitMap_t current_FontBitMap,unsigned int color) {
	int x;
	int y;
	unsigned char ucByte = 0;
	int i = 0;
	int bit;
	if (current_FontBitMap->iBpp == 1)
	{
		for (y = current_FontBitMap->iYTop; y < current_FontBitMap->iYMax; y++)
		{
			i = (y - current_FontBitMap->iYTop) * current_FontBitMap->iPitch;
			for (x = current_FontBitMap->iXLeft, bit = 7; x < current_FontBitMap->iXMax; x++)
			{
				if (bit == 7)
				{
					ucByte = current_FontBitMap->pucBuffer[i++];
				}
				
				if (ucByte & (1<<bit))
				{
					current_dir.show(current_dir.pcurrent_dev,x,y,color);
				}
				else
				{
				}
				bit--;
				if (bit == -1)
				{
					bit = 7;
				}
			}
		}
	}
	return 0;
}


static int  show_Freetype_Str(pFontBitMap_t current_FontBitMap,char * cstr,unsigned int color) {

	//这里还要补充判断坐标位置是否合法


	
	int n;
	size_t len = strlen(cstr) + 1;
	size_t converted = 0;
	wchar_t *WStr;
	WStr=(wchar_t*)malloc(len*sizeof(wchar_t));
	mbstowcs(WStr, cstr, 1000);

	if(current_FontBitMap->iCurOriginX >2048 || current_FontBitMap->iCurOriginY > 1200) {
		printf("字体初始位置设计不合理\n");
		current_FontBitMap->iCurOriginX = 50;
		current_FontBitMap->iCurOriginY = 50;
	}
	for ( n = 0; n < wcslen(WStr); n++ )
	{
			freetype.Font_Dir_Get_Bitmap(WStr[n],current_FontBitMap);			
			current_dir.show_Font(current_FontBitMap,color);
			current_FontBitMap->iCurOriginX = current_FontBitMap->iNextOriginX;
			current_FontBitMap->iCurOriginY = current_FontBitMap->iNextOriginY;						
	}

}

static void dev_clear_show(pDisplay_dev pcurrent_dev) {
	memset(pcurrent_dev->screen_args->base_addr,0,pcurrent_dev->screen_args->size);
}


